using UnityEngine;

public class DeathBringerSpellCastState : EnemyState
{
    private new DeathBringer Enemy => (DeathBringer)base.Enemy;
    private int amountOfSpells;
    private float spellTimer;

    public DeathBringerSpellCastState(Enemy enemy, string animName) : base(enemy, animName) { }

    public override void Enter()
    {
        base.Enter();
        amountOfSpells = Enemy.AmountOfSpells;
        // spellTimer = Enemy.SpellCooldown + 0.5f;
        spellTimer = 0.5f;
    }

    public override void Update()
    {
        base.Update();
        spellTimer -= Time.deltaTime;
        if (CanCast())
        {
            Enemy.CastSpell();
        }
        if (amountOfSpells <= 0)
        {
            StateMachine.ChangeTo(Enemy.Teleport);
        }
    }

    public override void Exit()
    {
        base.Exit();
        Enemy.lastTimeCast = Time.time;
    }

    private bool CanCast()
    {
        if (amountOfSpells > 0 && spellTimer <= 0)
        {
            amountOfSpells--;
            spellTimer = Enemy.SpellCooldown;
            return true;
        }
        return false;
    }
}
